//
//  ContrastCompareView.swift
//  ChanMaMa
//
//  Created by 戴泉明 on 2021/4/21.
//  Copyright © 2021 蝉妈妈-戴泉明. All rights reserved.
//

import UIKit
import SnapKit

class ContrastCompareView: UIView, UIScrollViewDelegate {
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.sectionHeaderHeight = 45
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.bounces = false
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = .none
        tableView.backgroundColor = self.backgroundColor
        tableView.contentInsetAdjustmentBehavior = .never
        return tableView
    }()
    
    var index:Int64 = 0
    var datas: [ContrastModel] = []
    weak var delegate: ContrastScrollDelegate?
    weak var cellHeightDelegate: ContrastCellHeightDelegate?
    var dataArr: [GroupParamsModel] = []
    var tableHeader: ContrastCompareTableHeader = {
        let header = ContrastCompareTableHeader()
        header.frame = CGRect.zero
        header.isUserInteractionEnabled = true
        return header
    }()
    var scrollView:UIScrollView = {
        let scroll = UIScrollView()
        scroll.bounces = false
        scroll.contentInsetAdjustmentBehavior = .never
        return scroll
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        addSubview(scrollView)
        scrollView.addSubview(tableView)
        scrollView.addSubview(tableHeader)
        
        scrollView.snp.makeConstraints { (make) in
            make.top.bottom.left.right.equalTo(self)
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(CONTRAST_HEADER_HEIGHT)
            make.right.left.bottom.equalTo(scrollView)
        }
        tableHeader.snp.makeConstraints { (make) in
            make.height.equalTo(CONTRAST_HEADER_HEIGHT)
            make.top.right.equalTo(self)
            make.left.equalTo(scrollView)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        delegate?.e_scrollViewDidScroll(scrollView)
    }
    
    func setDatas(datas:[ContrastModel]) {
        self.datas = datas
        scrollView.contentSize = CGSize(width: CGFloat(datas.count) * CONTRAST_ITEM_WIDTH, height: 0);
        scrollView.snp.remakeConstraints { (make) in
            make.top.bottom.left.right.equalTo(self)
        }
        tableHeader.backgroundColor = UIColor.white
        tableHeader.snp.remakeConstraints { (make) in
            make.height.equalTo(CONTRAST_HEADER_HEIGHT)
            make.top.right.equalTo(self)
            make.left.equalTo(scrollView)
        }
        tableView.snp.remakeConstraints { (make) in
            make.top.equalTo(tableHeader.snp_bottom)
            make.left.equalTo(scrollView)
            make.bottom.equalTo(self)//底部不能给到scrollview 否则出不来
            make.width.equalTo(CGFloat(datas.count) * CONTRAST_ITEM_WIDTH)
        }

        dataArr = datas[0].groupParamsViewModelList
        tableHeader.setdatas(datas: datas)
        tableView.reloadData()
        tableHeader.backgroundColor = self.backgroundColor
    }
}

extension ContrastCompareView:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return dataArr.count
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr[section].paramList.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeightDelegate?.e_tableViewCellHeight(indexPath) ?? 40
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ContrastCompareCell.cellWithTableView(tableView: tableView)
        cell.setDatas(datas: datas, indexPath: indexPath)
        cell.backgroundColor = cellHeightDelegate?.e_tableViewCellColor(indexPath) ?? self.backgroundColor
        return cell
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let sectionHeader = UIView()
        sectionHeader.backgroundColor = UIColor.init(ts_hexString: "F5F6F7")
        return sectionHeader
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        tableView.deselectRow(at: indexPath, animated: false)
    }
}

